# author Martin Bros

function do_createFile()
{
    local DEST="$1"
    
    if [ ! -w "$DEST" ]
    then
    	return
    fi
    
    local NAME
	read -p "Enter name of new file: " NAME
	
	while [ -e "$DEST/$NAME" ] && [ "$NAME" != "" ]
	do
		read -p "This file exists! Enter name of new file: " NAME
	done
	
	if [ "$NAME" == "" ]
	then
		return
	fi
	
    touch "$DEST/$NAME"
}

function do_createDir()
{
    local DEST="$1"
    
    if [ ! -w "$DEST" ]
    then
    	return
    fi
    
    local NAME
	read -p "Enter name of new directory: " NAME
	
	while [ -e "$DEST/$NAME" ] && [ "$NAME" != "" ]
	do
		read -p "This file exists! Enter name of new directory: " NAME
	done
	
	if [ "$NAME" == "" ]
	then
		return
	fi
	
    mkdir "$DEST/$NAME"
}

function do_delete()
{
    local DEST="$1"
    local FILE="$2"
	local CHAR
	
	read -n1 -p "Do you really want to delete this file? (y/n): " CHAR
	
	if [ "$CHAR" == "y" ] && [ -w "$DEST/$FILE" ]
	then
    	rm -rf "$DEST/$FILE"
    fi
}

function do_rename()
{
    local WHERE="$1"
    local FILE="$2"
    
    if [ ! -w "$WHERE" ]
    then
    	return
    fi
    
    local NEWNAME
    read -p "Enter new name: " NEWNAME

	while [ -e "$WHERE/$NEWNAME" ] && [ "$NEWNAME" != "" ]
	do
		read -p "This file exists! Enter name of new file: " NEWNAME
	done
	
	if [ "$NEWNAME" == "" ]
	then
		return
	fi
	
    mv "$WHERE/$FILE" "$WHERE/$NEWNAME"
}

function do_copy()
{
    local FROM="$1"
    local FILE="$2"
    local TO="$3"
	
	if [ ! -r "$FROM/$FILE" ] || [ ! -w "$TO" ]
    then
    	return
    fi
	
	if [ -e "$TO/$FILE" ]
	then
		read -n1 -p "This file alredy exists! Do you really want to owerwrite this file? (y/n): " CHAR
	
		if [ "$CHAR" != "y" ]
		then
			return
		fi
	fi
	
    cp -rf "$FROM/$FILE" "$TO"
}

function do_move()
{
    local FROM="$1"
    local FILE="$2"
    local TO="$3"
	
	if [ ! -r "$FROM/$FILE" ] || [ ! -w "$TO" ]
    then
    	return
    fi
	
	if [ -e "$TO/$FILE" ]
	then
		read -n1 -p "This file alredy exists! Do you really want to owerwrite this file? (y/n): " CHAR
	
		if [ "$CHAR" != "y" ]
		then
			return
		fi
	fi
	
    mv -f "$FROM/$FILE" "$TO"
}

function do_list()
{
    local DEST="$1"
    local HIDDEN="$2"
    FLAGS=""

    if [ "$HIDDEN" -eq 1 ]
	then
	    FLAGS="-A"
    fi

    ls -lQ --time-style=+%s $FLAGS "$DEST" | grep '\"' |
    awk '{	ind=index($0, "\"")
    		name=substr($0, ind+1, length($0)-ind)
    		name=substr(name, 1, index(name, "\"")-1)
    		gsub(" ",":;",name)
    		printf "%s %s %d %d\n", substr($1, 1, 1)=="d"?"d":"f", name, $6, substr($1, 1, 1)=="-"?$5:"0"}'
}
